<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item v-if="dataForm.id" label="学生学号" prop="num">
      <el-input v-model="dataForm.num" placeholder="学生学号"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="学生姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="学生姓名"></el-input>
    </el-form-item>
    <el-form-item label="学生性别" prop="gender">
      <el-radio v-model="dataForm.gender" label="0">女</el-radio>
      <el-radio v-model="dataForm.gender" label="1">男</el-radio>
    </el-form-item>
    <el-form-item label="头像" prop="img">
      <el-input v-model="dataForm.img" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="班级ID" prop="classId">
      <el-input v-model="dataForm.classId" placeholder="班级ID"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <!-- <el-input v-model="dataForm.status" placeholder="状态"></el-input> -->
      <el-radio v-model="dataForm.status" label="1">正常</el-radio>
      <el-radio v-model="dataForm.status" label="2">休学</el-radio>
      <el-radio v-model="dataForm.status" label="3">退学</el-radio>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          num: '',
          password: '',
          name: '',
          gender: '',
          img: '',
          createTime: '',
          updateTime: '',
          classId: '',
          status: '1',
          isDelete: ''
        },
        dataRule: {
          num: [
            { required: true, message: '学生学号不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '学生姓名不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '学生性别不能为空', trigger: 'blur' }
          ],
          img: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          classId: [
            { required: true, message: '班级ID不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/stu/student/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.num = data.student.num
                this.dataForm.password = data.student.password
                this.dataForm.name = data.student.name
                this.dataForm.gender = data.student.gender
                this.dataForm.img = data.student.img
                this.dataForm.createTime = data.student.createTime
                this.dataForm.updateTime = data.student.updateTime
                this.dataForm.classId = data.student.classId
                this.dataForm.status = data.student.status
                this.dataForm.isDelete = data.student.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/stu/student/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'num': this.dataForm.num,
                'password': this.dataForm.password,
                'name': this.dataForm.name,
                'gender': this.dataForm.gender,
                'img': this.dataForm.img,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'classId': this.dataForm.classId,
                'status': this.dataForm.status,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
